<?

require_once('DataObject/DomainModel.php');

class FilesTypes extends DomainModel {
	protected $id;	//primaryKey=yes, dataType=smallint, maxLength=, allowNulls=no
	protected $type;	//primaryKey=no, dataType=varchar, maxLength=50, allowNulls=no
}

class FilesTypesMapper extends ModelMapper {
	function __construct(){
		parent::__construct();
		$this->tableName = 'files_types';
		$this->selectViewName = 'v_files_types';
		$this->selectFields = '
				id,
				type
		';

		$this->findQuery = self::$DB->prepare("
			SELECT ".$this->selectFields."
			FROM ".$this->selectViewName."
			WHERE id = ?
		");
		$this->insertQuery = self::$DB->prepare("
			CALL p_add_files_types (@p_id,
				?
		)");


		$this->updateQuery = self::$DB->prepare("
			CALL p_upd_files_types (
				?,
				?

		)");

		$this->deleteQuery = self::$DB->prepare("
			CALL p_del_files_types (?)
		");

		$this->cacheTime = null;
	}

	protected function doLoad($array){
		$acuire = str_replace('Mapper', '', get_class($this));
		$obj = new $acuire($array['id']);
		$obj->setId($array['id']);
		$obj->setType($array['type']);

		return $obj;
	}

	function insert(DomainModel $obj){

		$values = array(
			$obj->getType()
		);

		$this->doStatement($this->insertQuery, $values);
		$obj->setId(self::$DB->getOne("SELECT @p_id"));
	}

	function update(DomainModel $obj){
		$values = array(
			$obj->getId(),
			$obj->getType()
		);

		$this->doStatement($this->updateQuery, $values);
	}

	function delete(DomainModel $obj){
		$this->doStatement($this->deleteQuery, array($obj->getId()));
	}

	function newId(){

		$row = self::$DB->getRow('SHOW TABLE STATUS LIKE files_types');
		return $row['Auto_increment'];

	}
}

class FilesTypesCollection extends ModelCollection {
	function add(FilesTypes $model){
		$this->doAdd($model);
	}
}

class FilesTypesCollectionExtended extends FilesTypesCollection {
	private $stmt;
	private $valueArray;
	private $mapper;
	private $run = false;

	function __construct(ModelMapper $mapper, $stmtHandle, $valueArray){
		parent::__construct();
		$this->stmt = $stmtHandle;
		$this->valueArray = $valueArray;
		$this->mapper = $mapper;
	}

	function notifyAccess(){
		if (!$this->run){
			$result = $this->mapper->doStatement($this->stmt, $this->valueArray,$this->mapper->getCacheTime());
			$this->init_db($result, $this->mapper);
			$this->run = true;
		}
	}
}

?>
